package mn.more.wits.client.util;

import net.mygwt.ui.client.widget.Info;

/**
 * @author <a href="mailto:mike.liu@aptechmongolia.edu.mn">Mike Liu</a>
 * @version $Id: InputValidator.java 5 2008-09-01 12:08:42Z mikeliucc $
 */
public final class InputValidator {

	// Per requirement: 6 to 25 character in length, alphanumeric, UNICODE, may include (._-@)
	public static final String JS_REGEX_USERNAME = "([\\" + "u0040\\" + "u002D\\" + "u002e\\" +
	                                               "u0030-\\" + "u0039\\" + "u0041-\\" + "u005a\\" +
	                                               "u005f\\" +
	                                               "u0061-\\" + "u007a\\" +
	                                               "u0400-\\" + "uffff]{6,25})";
	// Per requirement: Must not begin or end with spaces.
	public static final String JS_REGEX_USERNAME2 = "^[^\\s]+.*[^\\s]+$";
	// Per requirements: 8 to 25 characters in length, alphanumeric, UNICODE, punctuations and spaces
	public static final String JS_REGEX_PASSWORD = "([\\" + "u0020-\\" + "u007e\\" + "u0400-\\" + "uffff]{8,25})";
	// Per requirement: Must not begin or end with spaces.
	public static final String JS_REGEX_PASSWORD2 = "^[^\\s]+.*[^\\s]+$";
	// Per requirements: at least one punctuation and one number.
	// todo: the REAL regex is turned off to ease development and testing.
	// todo: But it REALLY should be turned back on for real app. use/testing!
	//public static final String JS_REGEX_PASSWORD3="(.*?)([\\d].*?[\\" + "u0021-\\" + "u002f\\" +
	//                                              "u003a-\\" + "u0040\\" + "u005b-\\" + "u0060]|[\\" +
	//                                              "u0021-\\" + "u002f\\" + "u003a-\\" + "u0040\\" +
	//                                              "u005b-\\" + "u0060].*?[\\d])(.*)";
	public static final String JS_REGEX_PASSWORD3 = ".*";

	private static final boolean DEBUG = false;

	private InputValidator() { }

	public static boolean isValidPassword(String text) {
		if (text == null || text.length() < 1) { return false; }

		boolean test1 = text.matches(JS_REGEX_PASSWORD);
		if (!test1) {
			if (DEBUG) { Info.show("isValidPassword", "failed on " + JS_REGEX_PASSWORD, ""); }
			return false;
		}

		boolean test2 = text.matches(JS_REGEX_PASSWORD2);
		if (!test2) {
			if (DEBUG) { Info.show("isValidPassword", "failed on " + JS_REGEX_PASSWORD, ""); }
			return false;
		}

		boolean test3 = text.matches(JS_REGEX_PASSWORD3);
		if (!test3) {
			if (DEBUG) { Info.show("isValidPassword", "failed on " + JS_REGEX_PASSWORD3, ""); }
			return false;
		} else {
			return true;
		}
	}

	public static boolean isValidUsername(String text) {
		if (text == null || text.length() < 1) { return false; }
		boolean test1 = text.matches(JS_REGEX_USERNAME);
		if (!test1) {
			if (DEBUG) { Info.show("isValidUsername", "failed on " + JS_REGEX_USERNAME, ""); }
			return false;
		}

		boolean test2 = text.matches(JS_REGEX_USERNAME2);
		if (!test2) {
			if (DEBUG) { Info.show("isValidUsername", "failed on " + JS_REGEX_USERNAME2, ""); }
			return false;
		} else {
			return true;
		}
	}
}
